package com.gjy.nwpufindseats.Fragment;

import org.apache.http.cookie.Cookie;

import com.gjy.nwpufindseats.R;
import com.gjy.nwpufindseats.util.JwNetSpider;
import com.gjy.nwpufindseats.util.WebProgressBar;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

@SuppressLint({ "ValidFragment", "SetJavaScriptEnabled" })
public class Fragment_webHtml extends Fragment {

	public static SharedPreferences preferences;
	public String xhString;
	public String passwordString;
	public String urlLost = "http://portal.nwpu.edu.cn/dcp/forward.action?path=dcp/apps/sso/jsp/ssoDcpSelf&appid=1071109";
	public static Cookie sessionCookie;
	public static CookieManager cookieManager;
	public static WebProgressBar webProgressBar;
	public Fragment_webHtml() {
	}


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		// file:///android_asset/index.html
		View rootView = inflater.inflate(R.layout.fragment_webhtml, container, false);
		preferences=getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
		webProgressBar = (WebProgressBar) rootView.findViewById(R.id.webkit);
		// 设置WebView支持JS
		webProgressBar.getSettings().setJavaScriptEnabled(true);
		// 设置当前WebView的编码
		webProgressBar.getSettings().setDefaultTextEncodingName("utf-8");
		
		Thread loginThread = new Thread(){
			public void run(){				
				 sessionCookie = JwNetSpider.lost(preferences.getString("xh", "0"), preferences.getString("dPassword", "0"));  
				 Fragment_more.lostDialog.dismiss();
				 
			}			
		};
		loginThread.start();
		//开启DOM 存储
		webProgressBar.getSettings().setDomStorageEnabled(true);
				// 启动Webview时加载的Url
				try {
					loginThread.join();
						if(!JwNetSpider.axgdBoolean)//网页打开正常，只能是密码错误
						{
							Toast.makeText(getActivity(), "网速不给力，无法得到认证！", Toast.LENGTH_SHORT).show();
							JwNetSpider.axgdBoolean=true;//恢复为true，下次给出正确提醒
							getActivity().finish();
						}
					CookieSyncManager.createInstance(getActivity());
					cookieManager = CookieManager.getInstance();
					if (sessionCookie != null) {  
					    //cookieManager.removeSessionCookie();  
					    String cookieString = sessionCookie.getName() + "=" + sessionCookie.getValue() + "; domain=" + sessionCookie.getDomain();  
					    cookieManager.setCookie(urlLost, cookieString);  
					    CookieSyncManager.getInstance().sync();  
					}
					webProgressBar.loadUrl(urlLost);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
		
		// WebView内链接跳转
				webProgressBar.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {

				// 当前webview内访问url
				view.loadUrl(url);
				// 返回true表示停留在本WebView（不跳转到系统的浏览器）
				return true;
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				// TODO Auto-generated method stub
				super.onPageFinished(view, url);
			}

		});				
		return rootView;
	}
	
}
